﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Bitirme.DAL.Entities;
using Bitirme.DAL.Enum;
using Bitirme.DAL.Data_Access;


namespace Bitirme.Controllers
{
    public class AssignmentHandler
    {
        /*** Assignmentları ProjeIdlere göre getirir,
         * employee ve workpackage'i sadece id leriyle beraber getirecek ***/
        public List<Assignment> GetAssignmentsByProjectID(int projectID)
        {
            return AssignmentCollection.GetAssignmentsByProjectID(projectID);
        }

        /*** Assignmentları EmployeeID lere göre getirir ***/
        public List<Assignment> GetAssignmentsByEmployeeID(int employeeID)
        {
            return AssignmentCollection.GetAssignmentsByEmployeeID(employeeID);

        }

        /*** Assignmentları WorkPackageID lere göre getirir ***/
        public List<Assignment> GetAssignmentsByWorkPackageID(int workpackageID)
        {
            return AssignmentCollection.GetAssignmentsByWorkPackageID(workpackageID);

        }

        public int InsertAssignments(List<Assignment> assignmentList) {

            //List<int> deletedAssignments = new List<int>();
 
            foreach (Assignment assignedItem in assignmentList)
            {
                AssignmentCollection.DeleteAssignmentsByWorkPackageID(assignedItem.WorkPackage.ID);
            }
            
            
            int assignmentID = -1;
            foreach (Assignment assignedItem in assignmentList) {


                int projectID = WorkPackageCollection.GetProjectIDByWorkPackageID(assignedItem.WorkPackage.ID);
                assignmentID = AssignmentCollection.InsertAssignment(assignedItem);
                AssignmentCollection.InsertAssignmentToProject(assignmentID, projectID);
            }

            return assignmentList.Count;
        
        }

        public Project GetProjectByWorkPackageID(int wpID) {
            return ProjectCollection.SelectProjectByID(WorkPackageCollection.GetProjectIDByWorkPackageID(wpID));
        }
    }
}